local zonglan = fk.CreateSkill {
  name = "lingling__zonglan",
}

Fk:loadTranslationTable {
  ["lingling__zonglan"] = "总揽",
  [":lingling__zonglan"] = "轮次开始时，你可以指定一名其他角色，其下两次获得一张锦囊牌时，你获得之，若为本轮第二次，其摸一张牌。",

  ["#lingling__zonglan-choose"] = "总揽：选择一名其他角色，其下两次获得一张锦囊牌后，你获得之。",
  ["@lingling__zonglan"] = "总揽",
}

zonglan:addEffect(fk.RoundStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(zonglan.name) and
        #player.room:getOtherPlayers(player, false) > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room:getOtherPlayers(player, false),
      skill_name = zonglan.name,
      prompt = "#lingling__zonglan-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, { tos = to })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:setPlayerMark(to, "@lingling__zonglan", 2)
  end,
})

zonglan:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(zonglan.name) then
      for _, move in ipairs(data) do
        if move.to and move.to:getMark("@lingling__zonglan") > 0 and
            move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(move.to:getCardIds("h"), info.cardId) and
                Fk:getCardById(info.cardId).type == Card.TypeTrick then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to and move.to:getMark("@lingling__zonglan") > 0 and
          move.toArea == Card.PlayerHand and not player.dead and not move.to.dead then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(move.to:getCardIds("h"), info.cardId) and
              Fk:getCardById(info.cardId).type == Card.TypeTrick and move.to:getMark("@lingling__zonglan") > 0 then
            room:removePlayerMark(move.to, "@lingling__zonglan", 1)
            room:moveCardTo(info.cardId, Card.PlayerHand, player, fk.ReasonPrey, zonglan.name, nil, false, player)
            if move.to:getMark("@lingling__zonglan") == 0 then
              move.to:drawCards(1, zonglan.name)
            end
          end
        end
      end
    end
  end,
})

zonglan:addLoseEffect(function(self, player, is_death)
  local room = player.room
  for _, p in ipairs(room.alive_players) do
    room:setPlayerMark(p, "@lingling__zonglan", 0)
  end
end)

return zonglan
